<?php

/**
 * Website Controller
 * @author Administrator
 */
class OrganInfoController {
	// TODO - Insert your code here
	private $path = "";
	private $lan;

	/**
	 */
	function __construct() {

		$this->path = "../src/module/admin/page_organinfo/temp/";
		
		if (isset( $_SESSION[SESSION_LANGUAGE])) {
			$this->lan = $_SESSION[SESSION_LANGUAGE];
		} else {
			$this->lan = DEFAULT_LANGAUGE;
		}
	}

	/**
	 * view form function
	 */
	public function viewForm() {
		
		// create xTemplate
		$template = new XTemplate( $this->path . $this->lan . "/form.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_ORGANINFO);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
		
		$db = new DBAccess();
		$info = $db->selectOneByCause( $GLOBALS['organinfoFullColArray'], ORGANINFO, array(ORGANINFO_LANGUAGE => $this->lan));
		
		if ($info == null || count( $info) == 0) {
			$info = array();
			$info[ORGANINFO_ID] = 0;
			$info[ORGANINFO_ORGANIZATION] = "";
			$info[ORGANINFO_ADDRESS] = "";
			$info[ORGANINFO_PHONENUMBER] = "";
			$info[ORGANINFO_FAXNUMBER] = "";
			$info[ORGANINFO_EMAIL] = "";
			$info[ORGANINFO_HOTLINE] = "";
			$info[ORGANINFO_MAPADDRESS] = "";
			$info[ORGANINFO_FACEBOOKSITE] = "";
			$info[ORGANINFO_LANGUAGE] = "";
			$info[ORGANINFO_EDITBY] = "";
		}
		
		// assign form item
		$template->assign( FORMTITLE, "Cấu hình thông tin website");
		$template->assign( FORMITEM, $info);
		$template->parse( PAGECONTENT . "." . FORMITEM);
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * submit form function
	 * @return multitype:string
	 */
	public function submitForm() {
		
		// reponse array
		$response_array = array();
		
		// get input
		$info[ORGANINFO_ID] = $_REQUEST[ORGANINFO_ID];
		$info[ORGANINFO_ORGANIZATION] = $_REQUEST[ORGANINFO_ORGANIZATION];
		$info[ORGANINFO_ADDRESS] = $_REQUEST[ORGANINFO_ADDRESS];
		$info[ORGANINFO_PHONENUMBER] = $_REQUEST[ORGANINFO_PHONENUMBER];
		$info[ORGANINFO_FAXNUMBER] = $_REQUEST[ORGANINFO_FAXNUMBER];
		$info[ORGANINFO_EMAIL] = $_REQUEST[ORGANINFO_EMAIL];
		$info[ORGANINFO_HOTLINE] = $_REQUEST[ORGANINFO_HOTLINE];
		$info[ORGANINFO_MAPADDRESS] = $_REQUEST[ORGANINFO_MAPADDRESS];
		$info[ORGANINFO_FACEBOOKSITE] = $_REQUEST[ORGANINFO_FACEBOOKSITE];
		$info[ORGANINFO_LANGUAGE] = $this->lan;
		$info[ORGANINFO_EDITBY] = $_SESSION[SESSION_USERNAME];
		
		// update data to database
		$db = new DBAccess();
		
		if ($info[ORGANINFO_ID] == 0) {
			if ($db->insertData( $info, ORGANINFO)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_ORGANINFO . "&act=" . PVALUE_ACTION_VIEW_FORM;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
			}
		} else {
			if ($db->updateData( $info, ORGANINFO)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_ORGANINFO . "&act=" . PVALUE_ACTION_VIEW_FORM;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
			}
		}
		
		// sleep(4);
		return $response_array;
	
	}

	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
}

?>